import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import '../../api/index.dart' show getBanners;

class SwiperDemo extends StatefulWidget {
  SwiperDemo({Key key}) : super(key: key);
  @override
  _SwiperDemoState createState() => _SwiperDemoState();
}

class _SwiperDemoState extends State<SwiperDemo> {
  List swiperList = [];
  @override
  void initState() {
    super.initState();
    this._getBanners();
  }

  _getBanners() async {
    var res = await getBanners();
    print(res as Map);
    print(res);
    if (res['code'] == 200) {
      setState(() {
        this.swiperList = res['banners'];
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SwiperDemo'),
      ),
      body: Container(
        padding: EdgeInsets.all(10),
        color: Colors.white,
        child: AspectRatio(
          aspectRatio: 10 / 3,
          child: Swiper(
            itemBuilder: (BuildContext context, int index) {
              return Image.network(
                swiperList[index]['imageUrl'],
                fit: BoxFit.cover,
              );
            },
            itemCount: swiperList.length,
          ),
        ),
      ),
    );
  }
}
